home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Inspectors / AIInspector / AIView.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  128 lines

  1.  
  2. /*
  3.  * AIView.m
  4.  *
  5.  * AIView is a subclass of View responsible for managing the NXEPSImageRep
  6.  * of the ai file and also updating the information fields on the display.
  7.  *
  8.  *
  9.  * You may freely copy, distribute, and reuse the code in this example.
  10.  * Both Terrence Talbot and Digital Tool Works disclaim any warranty
  11.  * of any kind, expressed or implied, as to its fitness for any particular use.
  12.  *
  13.  * Written by:  Terrence Talbot 
  14.  *    Created:  Oct/92
  15.  *
  16.  */
  17.  
  18. #import "AIView.h"
  19.  
  20. @implementation AIView:View
  21.  
  22. /* INIT/FREE METHODS */
  23.  
  24. - free;
  25. /*
  26.  * Free the image on our way out.
  27.  */
  28. {
  29.     [image free];
  30.     return [super free];
  31. }
  32.  
  33. /* PRIVATE METHODS */
  34.  
  35. - setImageToFilename:(const char *)filename;
  36. {
  37.     if (image) [image free];
  38.     image = [[NXEPSImageRep alloc] initFromSection:filename];
  39.     [self display];
  40.     
  41.     return self;
  42. }
  43.  
  44. - display
  45. {
  46.    char   aiScale[32];
  47.    
  48.    NXSetRect(&aiRect,0.0,0.0,0.0,0.0);
  49.    aspectRatio = 0.0;
  50.    imageScale = 0;
  51.    
  52.    [super display]; // calls drawSelf:: which sets aiRect and aspectRatio properly
  53.  
  54.    [widthField setFloatValue:aiRect.size.width];
  55.    [heightField setFloatValue:aiRect.size.height];
  56.    
  57.    imageScale = floor(aspectRatio * 100);
  58.    sprintf(aiScale,"%i%%",imageScale);
  59.    [scaleField setStringValue:aiScale];
  60.  
  61.    return self;   
  62. }
  63.    
  64. - drawSelf:(NXRect *)rects :(int)rectCount;
  65. {
  66.    NXRect imageRect;
  67.    
  68.    [image getBoundingBox:&aiRect];
  69.    
  70.    // here's a lot of stuff to preserve the aspect ratio and calculate the scale
  71.    
  72.    // if it's bigger than the bounds, bring it back to the bounds
  73.    if ( (aiRect.size.width > bounds.size.width) && 
  74.         (aiRect.size.height > bounds.size.height) ) {
  75.       if ( (bounds.size.width / aiRect.size.width) < 
  76.            (bounds.size.height / aiRect.size.height) ) {
  77.          aspectRatio = (bounds.size.width / aiRect.size.width);
  78.          NXSetRect(&imageRect,
  79.             bounds.origin.x,
  80.         ((bounds.size.height - (aiRect.size.height * aspectRatio)) / 2),
  81.         bounds.size.width,
  82.         (aiRect.size.height * aspectRatio));
  83.       }
  84.       else {
  85.          aspectRatio = (bounds.size.height / aiRect.size.height);
  86.          NXSetRect(&imageRect,
  87.             ((bounds.size.width - (aiRect.size.width * aspectRatio)) /2),
  88.         bounds.origin.y,
  89.         (aiRect.size.width * aspectRatio),
  90.         bounds.size.height);
  91.       }
  92.    }
  93.    else // the width has to be brought back to the bounds and the height scaled accordingly
  94.       if ( aiRect.size.width > bounds.size.width ) {
  95.          aspectRatio = (bounds.size.width / aiRect.size.width);
  96.          NXSetRect(&imageRect,
  97.         bounds.origin.x,
  98.         ((bounds.size.height - (aiRect.size.height * aspectRatio)) / 2),
  99.         bounds.size.width,
  100.         (aiRect.size.height * aspectRatio));
  101.       }
  102.       else // the height has to be brought back to the bounds and the width scaled accordingly
  103.          if ( aiRect.size.height > bounds.size.height ) {
  104.         aspectRatio = (bounds.size.height / aiRect.size.height);
  105.             NXSetRect(&imageRect,
  106.                ((bounds.size.width - (aiRect.size.width * aspectRatio)) /2),
  107.            bounds.origin.y,
  108.            (aiRect.size.width * aspectRatio),
  109.            bounds.size.height);
  110.      }
  111.      else { // it's smaller than the bounds, so center the thing
  112.         aspectRatio = 1.0;
  113.             NXSetRect(&imageRect,
  114.            ((bounds.size.width - aiRect.size.width)/2),
  115.            ((bounds.size.height - aiRect.size.height)/2),
  116.            aiRect.size.width,
  117.            aiRect.size.height);
  118.      }
  119.       
  120.    NXEraseRect(&bounds);  /* to be sure to clear background */
  121.  
  122.    [image drawIn:&imageRect];
  123.  
  124.    return self;
  125. }
  126.  
  127. @end
  128.